<template>
    <div id="nav-menu">
      <el-menu
        class="el-menu-demo"
        mode="horizontal"
        background-color="#545c64"
        text-color="#fff"
        default-active="home"
        active-text-color="#ffd04b">
        <el-row >
          <router-link to="/">
            <el-col :span="1.5" :offset=4>
              <el-menu-item index="home">
                <i class="el-icon-s-home"></i>首页
              </el-menu-item>
            </el-col>
          </router-link>

            <el-col :span="1.5" >
              <el-menu-item index="shop" @click="toStore">
                <i class="el-icon-shopping-cart-1"></i>商城
              </el-menu-item>
            </el-col>
            <router-link :to="lr" v-if="!isLogin">
            <el-col :span="1.5" :offset=12 @click="doLogin">
              <el-menu-item index="login">
                <i class="el-icon-user"></i><span id="loginButton" @click="doLogin">登录</span> / <span id="registerButton" @click="doRegister">注册</span>
              </el-menu-item>
            </el-col>
          </router-link>
          <router-link to="/me" v-if="isLogin">
            <el-col :span="1.5" :offset=12>
              <el-menu-item>
                <i class="el-icon-user"></i>
                <el-dropdown @command="handleCommand">
                  <span class="el-dropdown-link" style="color:white">
                    {{username}}<i class="el-icon-arrow-down el-icon--right"></i>
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="exit">退出</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-menu-item>
            </el-col>
          </router-link>
        </el-row>
      </el-menu>
    </div>
</template>

<script>
  export default {
    name: "NavMenu",
    data() {
      return {
        // Login or Register  登录/注册按钮
        lr: ''
      };
    },
    props: {
      user: {
        type: String,
        default() {
          return null
        }
      }
    },
    methods: {
      doLogin() {
        this.lr = '/login'
      },
      doRegister() {
        this.lr = '/register'
      },
      handleCommand(command) {
        if ('exit' === command) {
          this.$emit('exit')
        }
      },
      toStore() {
        if (!this.isLogin) {
          this.$notify.error({
            title: '无此权限',
            message: '请先登录',
            offset: 100,
            duration: 2000
          })
          this.$router.push('/login')
        } else {
          this.$router.push('/store')
        }
      }
    },
    computed: {
      isLogin() {
        return this.user != null
      },
      username() {
        let userList = JSON.parse(window.localStorage.getItem('user'))
        if (userList[this.user].hasOwnProperty('name')) {
          return userList[this.user].name
        }
        return this.user
      }
    }
  }
</script>

<style scoped>
  #nav-menu {
    width: 100vw;
  }
  #loginButton:hover {
    text-decoration: underline;
  }
  #registerButton:hover {
    text-decoration: underline;
  }
</style>
